package cn.dust.design.pattern.creational.singleton;

import java.io.*;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

/**
 * @Author: dust
 * @Date: 2019/11/25 18:59
 */
public class Test {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
//        LazySingleton lazySingleton=LazySingleton.getInstance();

//        Thread t1=new Thread(new T());
//        Thread t2=new Thread(new T());
//        t1.start();
//        t2.start();
//        System.out.println("program end");

//        HungrySingleton instance=HungrySingleton.getInstance();
//        EnumInstance instance=EnumInstance.getInstance();
//        instance.setData(new Object());
//        try {
//            ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream("singleton_file"));
//            oos.writeObject(instance);
//            File file=new File("singleton_file");
//
//            ObjectInputStream ois=new ObjectInputStream(new FileInputStream(file));
//
//            HungrySingleton newInstance=(HungrySingleton) ois.readObject();
//            EnumInstance newInstance=(EnumInstance) ois.readObject();
//
//            System.out.println(instance.getData());
//            System.out.println(newInstance.getData());
//            System.out.println(instance.getData()==newInstance.getData());
//
//
//        } catch (IOException | ClassNotFoundException e) {
//            e.printStackTrace();
//        }

//        Class objectClass=HungrySingleton.class;
//        Class objectClass=StaticInnerClassSingleton.class;

//        Class objectClass=LazySingleton.class;
//
//        Constructor constructor= objectClass.getDeclaredConstructor();
//        constructor.setAccessible(true);
//
//        HungrySingleton instance=HungrySingleton.getInstance();
//        HungrySingleton newInstance= (HungrySingleton) constructor.newInstance();
//        StaticInnerClassSingleton instance=StaticInnerClassSingleton.getInstance();
//        StaticInnerClassSingleton newInstance= (StaticInnerClassSingleton) constructor.newInstance();

//        LazySingleton instance=LazySingleton.getInstance();
//        LazySingleton newInstance= (LazySingleton) constructor.newInstance();
//
//        System.out.println(instance);
//        System.out.println(newInstance);
//        System.out.println(instance==newInstance);

//        Class.forName(HungrySingleton.class.getName());


    }
}
